package com.chinawayltd.ews.sample.service.app.impl;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.chinawayltd.ews.sample.mapper.TTaskAnomalousEventMapper;
import com.chinawayltd.ews.sample.model.TTaskAnomalousEvent;
import com.chinawayltd.ews.sample.service.app.AppService;

/**
 * @program: greeProject
 * @description 
 * @author: chenxin
 * @create: 2018年5月4日 上午11:24:34
 */
@Service
public class AppServiceImpl implements AppService {
	@Autowired
	private TTaskAnomalousEventMapper tTaskAnomalousEventMapper;

	@Override
	@Transactional
	public int addEvent(TTaskAnomalousEvent event) throws Exception{
		Date date = new Date();
		event.setReportTime(date);
		event.setCreateTime(date);
		event.setUpdateTime(date);
		int result = tTaskAnomalousEventMapper.insertSelective(event);
        if(result > 0){
        	/*TLog log = new TLog();
    		log.setDataId();
    		log.setTableName();
    		log.setOperationType(2);
    		log.setCreateTime(new Date());
    		tLogMapper.insertSelective(log);// 增加日记*/
        }
		return result;
	}

	@Override
	public String getAddrByBaidu(String lng, String lat, String ak) throws Exception {
		String url = "http://api.map.baidu.com/geocoder/v2/?location=" + lat + "," + lng + "&output=json&ak=" + ak;
		System.out.println("根据经纬度查询地址："+url);
		StringBuilder json = new StringBuilder();
		URL oracle = new URL(url);
		URLConnection uc = oracle.openConnection();
		BufferedReader in = new BufferedReader(new InputStreamReader(uc.getInputStream()));
		String inputLine = null;
		while ((inputLine = in.readLine()) != null) {
			json.append(inputLine);
		}
		in.close();
		JSONObject obj = JSONObject.parseObject(json.toString());
		if (obj.get("status").toString().equals("0")) {
			return obj.getJSONObject("result").getString("formatted_address") +"("+ obj.getJSONObject("result").getString("sematic_description")+")";
		} else {
			return null;
		}
	}
	
	
}
